﻿namespace Site2AppWP8
{
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Phone.Controls;

    public abstract class Overlay : UserControl
    {
        private PhoneApplicationPage mainPage;

        public Overlay(PhoneApplicationPage mainPage)
        {
            this.mainPage = mainPage;
            this.mainPage.OrientationChanged += this.MainPage_OrientationChanged;
        }

        public virtual bool NavigateOnBack
        {
            get { return true; }
        }

        protected abstract void RefreshLayout(double height, double width);

        private void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Portrait) == PageOrientation.Portrait)
            {
                // If in portrait mode...
                this.RefreshLayout(Application.Current.Host.Content.ActualHeight - this.mainPage.ApplicationBar.DefaultSize, Application.Current.Host.Content.ActualWidth);
            }
            else
            {
                // If in landscape mode...
                this.RefreshLayout(Application.Current.Host.Content.ActualWidth, Application.Current.Host.Content.ActualHeight - this.mainPage.ApplicationBar.DefaultSize);
            }
        }
    }
}
